package org.rency.crawler.scheduler.support;

import java.util.concurrent.RejectedExecutionHandler;
import javax.annotation.Resource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 描述: ${TODO}<br>
 * 最近修改时间:2018/2/9 16:54<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2018/2/9 16:54
 */
@Configuration
public class TaskSchedulerConf {

  @Resource(name = "listenerRejectedExecutionHandler")
  private RejectedExecutionHandler listenerRejectedExecutionHandler;

  @Bean
  @ConfigurationProperties(prefix = "crawler.scheduler.listener.task")
  public TaskSchedulerProperties taskSchedulerProperties(){
    return new TaskSchedulerProperties();
  }

  @Bean(name = "listenerTaskScheduler")
  public ThreadPoolTaskExecutor getListenerTaskScheduler(TaskSchedulerProperties taskSchedulerProperties){
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(taskSchedulerProperties.getCorePoolSize());
    executor.setMaxPoolSize(taskSchedulerProperties.getMaxPoolSize());
    executor.setQueueCapacity(taskSchedulerProperties.getQueueCapacity());
    executor.setThreadNamePrefix(taskSchedulerProperties.getPrefixName());
    executor.setRejectedExecutionHandler(listenerRejectedExecutionHandler);
    executor.initialize();
    return executor;
  }
}
